package Controlador;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;
import java.util.Vector;

import javax.swing.DefaultComboBoxModel;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.table.AbstractTableModel;
import javax.swing.table.TableModel;

import Vista.NewJDialogCategoria;
import Vista.NewJDialogOrden;
import Vista.NewJDialogProducto;
import Modelo.*;

public class ControladorCtegoria{
	private NewJDialogCategoria cate;
	
	public ControladorCtegoria(JFrame frm) {
		cate = new NewJDialogCategoria (frm, this);
		
		cate.getjBtoIncluir().addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent e) {
				if (cate.getjTDescripcion().getText().isEmpty() ||
						cate.getjTNombre().getText().isEmpty() ||
						cate.getjTxtCodigo().getText().isEmpty()) {
						JOptionPane.showMessageDialog(null, "Rellene los campos de textos.");
						return;
					}
				CategoriaDAO dao = new CategoriaDAO();
				dao.RegistrarCategoria((new Categoria(cate.getjTxtCodigo().getText(), cate.getjTNombre().getText(), cate.getjTDescripcion().getText())));
				cate.dispose();
				JOptionPane.showMessageDialog(null, " Categoria registrado", "Informacion", JOptionPane.INFORMATION_MESSAGE);
			}
		});
		cate.setVisible(true);
		cate.getjBtoEliminar().addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent e) {
				CategoriaDAO dao = new CategoriaDAO();
				dao.EliminarCategoria((new Categoria(cate.getjTxtCodigo().getText(), cate.getjTNombre().getText(), cate.getjTDescripcion().getText())));
				cate.dispose();
				JOptionPane.showMessageDialog(null, " Categoria ELiminada", "Informacion", JOptionPane.INFORMATION_MESSAGE);
			}
		});
		cate.getjBtoBuscarCate().addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent e) {
				CategoriaDAO dao = new CategoriaDAO();		
				
				Categoria ca = dao.ConsultarCategoria(cate.getjTxtCodigo().getText()); //consultar
				if (ca == null)
					JOptionPane.showMessageDialog(null, "No existe.");
				else {
					cate.getjTNombre().setText(ca.getNombre());
					cate.getjTDescripcion().setText(ca.getDescripcion());
				}
			}
		});
		cate.getjBtoEliminar().addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent e) {
				CategoriaDAO dao = new CategoriaDAO();
				dao.EliminarCategoria(((new Categoria(cate.getjTxtCodigo().getText(), cate.getjTNombre().getText(), cate.getjTDescripcion().getText()))));
				cate.dispose();
				JOptionPane.showMessageDialog(null, " Categoria eliminada", "Informacion", JOptionPane.INFORMATION_MESSAGE);			
	       }
		});
		cate.getjBtoSalir().addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent e) {
				cate.dispose();
			}
		});
	}
}
